package uk.org.scuts.bantstadium2;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.os.Process;

public class CharManager {
	
	// Constructors
	public CharManager(Document doc) {
		_map = new HashMap<String, CharBase>();
		NodeList nList = doc.getElementsByTagName("character");
	 
		for (int i = 0; i < nList.getLength(); i++) {
	 
			String name;
			String gender;
			String attack;
			String defense;
			String cunning;
			String hp;
			String speed;
			String attack0 = "Punch";
			String attack1 = "Kick";
			String attack2 = "Slap";
			String attack3 = "FlipOff";
			
			Node nNode = nList.item(i);
	 
			if (nNode.getNodeType() == Node.ELEMENT_NODE) {
	 
				Element eElement = (Element) nNode;
				name = eElement.getElementsByTagName("name").item(0).getTextContent();
				gender = eElement.getElementsByTagName("gender").item(0).getTextContent();
				attack = eElement.getElementsByTagName("attack").item(0).getTextContent();
				defense =eElement.getElementsByTagName("defense").item(0).getTextContent();
				cunning = eElement.getElementsByTagName("cunning").item(0).getTextContent();
				hp = eElement.getElementsByTagName("hp").item(0).getTextContent();
				speed = eElement.getElementsByTagName("speed").item(0).getTextContent();
				
				Node aNode = eElement.getElementsByTagName("attacks").item(0);
				if (aNode != null) {
					NodeList aList = ((Element) aNode).getElementsByTagName("item");
					attack0 = aList.item(0).getTextContent();
					attack1 = aList.item(1).getTextContent();
					attack2 = aList.item(2).getTextContent();
					attack3 = aList.item(3).getTextContent();
				}
				_map.put(name, new CharBase(gender,attack,defense,cunning,hp,speed,attack0,attack1,attack2,attack3));
			}	
		}
	}
	
	// Methods
	public void Replace (String oldName, String newName, CharBase base)
	{
		_map.remove(oldName);
		_map.put(newName, base);
	}
	
	public void Add (String newName, CharBase base)
	{
		_map.put(newName, base);
	}
	
	public void Delete (String oldName)
	{
		_map.remove(oldName);
	}
	
	public CharBase at(String key) {
		return _map.get(key);
	}
	
	public Document ToXML()
	{
		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = null;
		try {
			docBuilder = docFactory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
			Process.killProcess(Process.myPid());
		}
		
		// root elements
		Document doc = docBuilder.newDocument();
		Element rootElement = doc.createElement("characters");
		doc.appendChild(rootElement);
		
		Iterator<Map.Entry<String, CharBase>> ite = _map.entrySet().iterator();
	    while (ite.hasNext()) {
	        Map.Entry<String, CharBase> pairs = (Map.Entry<String, CharBase>)ite.next();
	        String nameString = pairs.getKey();
	        CharBase base = pairs.getValue();
	 
			// character elements
			Element oneChar = doc.createElement("character");
			rootElement.appendChild(oneChar);
	 
			// name elements
			Element name = doc.createElement("name");
			name.appendChild(doc.createTextNode(nameString));
			oneChar.appendChild(name);
			
			// gender elements
			Element gender = doc.createElement("gender");
			String genderString = "M";
			if (base._gender == Character.Gender.FEMALE)
			{
				genderString = "F";
			}
			gender.appendChild(doc.createTextNode(genderString));
			oneChar.appendChild(gender);
			
			// attack elements
			Element attack = doc.createElement("attack");
			attack.appendChild(doc.createTextNode(base._attack));
			oneChar.appendChild(attack);
			
			// defense elements
			Element defense = doc.createElement("defense");
			defense.appendChild(doc.createTextNode(base._defense));
			oneChar.appendChild(defense);
			
			// cunning elements
			Element cunning = doc.createElement("cunning");
			cunning.appendChild(doc.createTextNode(base._cunning));
			oneChar.appendChild(cunning);
			
			// hp elements
			Element hp = doc.createElement("hp");
			hp.appendChild(doc.createTextNode(base._hp));
			oneChar.appendChild(hp);
			
			// speed elements
			Element speed = doc.createElement("speed");
			speed.appendChild(doc.createTextNode(base._speed));
			oneChar.appendChild(speed);
			
			// attacks elements
			Element attacks = doc.createElement("attacks");
			oneChar.appendChild(attacks);
			
			// item elements
			Element item0 = doc.createElement("item");
			item0.appendChild(doc.createTextNode(base._attack0));
			attacks.appendChild(item0);
			Element item1 = doc.createElement("item");
			item1.appendChild(doc.createTextNode(base._attack1));
			attacks.appendChild(item1);
			Element item2 = doc.createElement("item");
			item2.appendChild(doc.createTextNode(base._attack2));
			attacks.appendChild(item2);
			Element item3 = doc.createElement("item");
			item3.appendChild(doc.createTextNode(base._attack3));
			attacks.appendChild(item3);
	    }
		
		return doc;
	}
	
	// Members
	Map<String, CharBase> _map;
}
